﻿				var tabs = new gadgets.TabSet(__MODULE_ID__, "main");
				var prefs = new gadgets.Prefs();
				var selectedTab = prefs.getString("selectedTab");
				var refreshinterval = prefs.getString("refreshinterval");
				var fetchsecfocus = prefs.getBool("fetchsecfocus");
				var newAlert = false;
				var backend = new Object();
				var entries = new Array();
				
				if(refreshinterval == "")
				{
					refreshinterval = "1";
				}
				else
				{
					refreshinterval = refreshinterval * 60 * 1000
				}
				
				var aktiv = window.setInterval("doReload()", refreshinterval);
				
				String.prototype.startsWith = function(str) 
					{return (this.match("^"+str)==str)};
					
				String.prototype.trim = function(){return 
					(this.replace(/^[\s\xA0]+/, "").replace(/[\s\xA0]+$/, ""))};
					
				String.prototype.endsWith = function(str) 
					{return (this.match(str+"$")==str)};

				function init() 
				{ 
					var secfocus = prefs.getBool("fetchsecfocus");
					
					showTab('grdSecFoc','none');
					
					tabs.addTab("OSVDB Results", 
					{
						contentContainer: document.getElementById("main_id")
					});
					if(fetchsecfocus)
					{
						tabs.addTab("Security Focus Results", 
						{
							contentContainer: document.getElementById("secfoc_id")
						});
						showTab('grdSecFoc','');
						getContent("secfocus");
					}	
					tabs.addTab("live Search", 
					{
					contentContainer: document.getElementById("lsearch_id")
					});
					
					getContent();
					
					selectTab();
				};
    
				function getContent()
				{
					if(arguments[0] != null)
					{
						switch(arguments[0])
						{
							case 'secfocus': var params = {};  
											 params[gadgets.io.RequestParameters.CONTENT_TYPE] = gadgets.io.ContentType.DOM;  
											 var url = "http://www.securityfocus.com/rss/vulnerabilities.xml";  
											 gadgets.io.makeRequest(url, response_secfoc, params);
											 break;
						}
					}
					else
					{
						var params = {};  
						params[gadgets.io.RequestParameters.CONTENT_TYPE] = gadgets.io.ContentType.DOM;
						
							var url = "http://osvdb.org/api/latest_rss_disc/sOQ8iRSPjh2HtIikW8jKWScJLD";  
						
						gadgets.io.makeRequest(url, response, params);
					}
				};
				
				function response_secfoc(obj) 
				{    
					var domdata = obj.data;
					var vulnlist = domdata.getElementsByTagName("item");
            		var vuln = 0;
            
					for (var i = 0; i < vulnlist.length ; i++) 
					{
						var nodeList = vulnlist.item(i).childNodes;
					
						for (var j = 0; j < nodeList.length ; j++) 
						{
							var node = nodeList.item(j);
							if (node.nodeName == "title") 
							{
								var title = node.firstChild.nodeValue;
								
							}
							if (node.nodeName == "link") 
							{
								var lnk = node.firstChild.nodeValue;
								var pos = lnk.lastIndexOf('/');
								var bid = lnk.substring(pos + 1, lnk.length);
							}
							if (node.nodeName == "description") 
							{
								var descr = node.firstChild.nodeValue;
							}
							if (node.nodeName == "pubDate") 
							{
								var pubDate = node.firstChild.nodeValue; 
								vuln = 0;
							}				
						}
			
						var flag = 0;
						
        				if(flag == 0)
        				{
            				var newrow = document.getElementById("grdSecFoc").insertRow(-1);
            				newrow.style.backgroundColor = "#F5F5DC";
							newcell = newrow.insertCell(0); newcell.innerHTML = '<a href="' + lnk + '" target="_blank">' + bid + '</a>';
							newcell = newrow.insertCell(0); newcell.innerHTML = title;
							newcell = newrow.insertCell(0); newcell.innerHTML = pubDate;
            				flag = 1;
        				}
        				else 
        				{
            				var newrow = document.getElementById("grdSecFoc").insertRow(-1);
            				newrow.style.backgroundColor = "#ADD8E6";
							newcell = newrow.insertCell(0); newcell.innerHTML = '<a href="' + lnk + '" target="_blank">' + bid + '</a>';
							newcell = newrow.insertCell(0); newcell.innerHTML = title;
							newcell = newrow.insertCell(0); newcell.innerHTML = pubDate;
            				flag = 0;
        				}	
					} 		
				};

				function response(obj) 
				{    
					var domdata = obj.data;
					var vulnlist = domdata.getElementsByTagName("item");
            
					for (var i = 0; i < vulnlist.length ; i++) 
					{
						var nodeList = vulnlist.item(i).childNodes;
					
						for (var j = 0; j < nodeList.length ; j++) 
						{
							var node = nodeList.item(j);
							if (node.nodeName == "title") 
							{
								var title = node.firstChild.nodeValue;
							}
							if (node.nodeName == "pubDate") 
							{
								var discdate = node.firstChild.nodeValue;
							}
							if (node.nodeName == "link") 
							{ 
								var lnk = node.firstChild.nodeValue;
								var pos = lnk.lastIndexOf('/');
								var osvdbid = lnk.substring(pos + 1, lnk.length);
							}				
						}
			
						var flag = 0;
						
        				if(flag == 0)
        				{
            				var newrow = document.getElementById("grdOSVDB").insertRow(-1);
            				newrow.style.backgroundColor = "#F5F5DC";
							newcell = newrow.insertCell(0); newcell.innerHTML = '<a href="' + lnk + '" target="_blank">' + osvdbid + '</a>';
							newcell = newrow.insertCell(0); newcell.innerHTML = title;
							newcell = newrow.insertCell(0); newcell.innerHTML = discdate
            				flag = 1;
        				}
        				else 
        				{
            				var newrow = document.getElementById("grdOSVDB").insertRow(-1);
            				newrow.style.backgroundColor = "#ADD8E6";
							newcell = newrow.insertCell(0); newcell.innerHTML = '<a href="http://osvdb.org/show/osvdb/' + osvdbid + '" target="_blank">' + osvdbid + '</a>';
							newcell = newrow.insertCell(0); newcell.innerHTML = title;
							newcell = newrow.insertCell(0); newcell.innerHTML = day + "." + month + "." + year + ", " + hours + ":" + minutes;
            				flag = 0;
        				}	
					} 		
				};
    
				function sendMessage(source)
				{
					var msg = new gadgets.MiniMessage(__MODULE_ID__);
	     
					var msg_text = "There was at least one new or updated vulnarability from following source(s):\n\n"

					if(source == "OSVDB")
					{
						msg_text += "OSVDB";
					}
					elseif(source == "secfocus")
					{
						msg_text += "Security Focus";	
					}
      
					msg.createDismissibleMessage(msg_text);
     
				};
				
				function do_query(qstring, qtype)
				{
					switch(qtype)
					{
						case "Vendor": window.open('http://osvdb.org/vendors/search?name=' + qstring);
										break;
						case "Product": window.open('http://osvdb.org/search?request=' + qstring);
										break;
						case "OSVDB-ID": window.open('http://osvdb.org/show/osvdb/' + qstring);
										break;
						default: window.open('http://osvdb.org/search?request=' + qstring);
										break;
					}
				};
				
				function doReload()
				{
					alert('Reload');
					//location.reload();	
				};
				
				function showTab(tabname,dis) 
				{
					document.getElementById(tabname).style.display = dis;
				}
				
				function selectTab() 
				{
					if(selectedTab == null)
					{    
						tabs.setSelectedTab(0);
					}
					else
					{
						tabs.setSelectedTab(selectedTab);	
					}
				}

				gadgets.util.registerOnLoadHandler(init);  